package com.css.fxfzysx.constants;


import com.css.fxfzysx.base.exception.ParamException;
import com.css.fxfzysx.modules.ysxManagement.repository.RcStructureRepository;
import com.css.fxfzysx.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

public  enum Src2Enum {
    EIGHT(20.0, 7),

    FIVE(7.0, 4),

    FORE(5.0, 3),


    NINE(25.0, 8),

    ONE(0.0, 0),

    SEVEN(15.0, 6),

    SIX(10.0, 5),

    THREE(3.0, 2),

    TWO(1.0, 1);


    private double state;

    private int index;

    private static final double arr[][] = {{0.80, 1.00, 1.28, 1.50, 1.70, 1.95, 2.32, 0.00, 0.00}, {0.00, 1.18, 1.54, 1.90, 2.20, 2.65, 3.40, 4.15, 4.90}, {0.00, 1.18, 1.54, 1.90, 2.20, 2.65, 3.40, 4.15, 4.90}, {0.00, 1.25, 1.75, 2.25, 2.60, 3.10, 3.95, 4.80, 0.00}};

    private static final double arr1[] = {0.0,1.0,3.0,5.0,7.0,10.0,15.0,20.0,25.0};


    Src2Enum(double state, int index) {
        this.state = state;
        this.index = index;
    }


    private static final TreeMap<Double, Src2Enum> DOUBLE_SRC_ENUM_TREE_MAP = new TreeMap<>();


    static {
        for (Src2Enum srcEnum : Src2Enum.values()) {
            DOUBLE_SRC_ENUM_TREE_MAP.put(srcEnum.state, srcEnum);
        }
    }

    public static double getSrcEnumByIndex(int index1,double state) {
        Map.Entry<Double, Src2Enum> entry = DOUBLE_SRC_ENUM_TREE_MAP.floorEntry(state);
        if (PlatformObjectUtils.isNotEmpty(entry)){
            Optional<Src2Enum> optional = Optional.of(entry.getValue());
            int index2 = optional.get().getIndex();
            if(state>25){
                throw new ParamException("参数输入异常，请检查后重新输入！");
            }
            double a1 = arr[index1][index2];


            double a2 = arr[index1][index2+1];


            double b1 = arr1[index2];

            double b2 = arr1[index2+1];

            double k = (a2-a1)/(b2-b1);

            double b = a1-k*b1 ;

            double y = k * state + b ;

            return y ;
        }
        return 0.00 ;

    }

    public double getState() {
        return this.state;
    }

    public int getIndex() {
        return this.index;
    }


}
